package com.kasax.plugin.app

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.kasax.plugin.app.transform.AddLogTransform
import com.kasax.plugin.app.transform.ClickLogInjectTransform
import com.kasax.plugin.app.transform.CustomLogTransform
import com.kasax.plugin.app.transform.MethodCostTimeTransform
import com.kasax.plugin.app.transform.MethodLifecycleTransform
import com.kasax.plugin.app.transform.MethodLifecycleTransformV2
import com.kasax.plugin.app.transform.NewInjectTransform
import com.kasax.plugin.app.transform.SimpleAsmTransfrom
import com.kasax.plugin.app.transform.SubModuleInitTransform
import org.gradle.api.Plugin
import org.gradle.api.Project

class AppMainPlugin implements Plugin<Project> {
    private Project project;

    @Override
    void apply(Project project) {
        println("handle AppMainPlugin apply")
        this.project = project
        handleRegisterTransform()
    }

    void handleRegisterTransform() {
        println("handle AppMainPlugin handleRegisterTransform")
//        def extensionFun = project.extensions.create("trackTime", GrokTrackTimeExtension.class)
        if (project.plugins.hasPlugin(AppPlugin)) {
            println("use in app")
            def appExtension = project.extensions.getByType(AppExtension)
            println("prepare exec registerTransform")
            // 日志插桩
            appExtension.registerTransform(new CustomLogTransform())
//            appExtension.registerTransform(new SimpleAsmTransfrom())
            // 耗时统计
//            appExtension.registerTransform(new MyCustomTransform2()) // NG
            // toast插桩
//            appExtension.registerTransform(new NewInjectTransform(project))
            // 日志插桩
//            appExtension.registerTransform(new AddLogTransform())
            // 耗时方法统计
//            appExtension.registerTransform(new MethodCostTimeTransform(project))
//            appExtension.registerTransform(new MethodLifecycleTransformV2())
//            appExtension.registerTransform(new MethodLifecycleTransform())
//            appExtension.registerTransform(new InjectTransform(project))
            // 点击日志插入
//            appExtension.registerTransform(new ClickLogInjectTransform(project))
            // 子模块初始化
//            appExtension.registerTransform(new SubModuleInitTransform())

            println("registerTransform complete")
        } else {
            println("use in other module")
        }
    }
}